<template>
  <el-form-item label="单选：" label-width="100px">
    <el-radio-group v-model="radioItem" @change="radioChange">
      <div v-for="(item, index) in optionList" :key="index" class="item">
        <el-radio :label="item.code">{{ item.code }}:</el-radio>
        <el-input v-model="item.title" class="inputbox"></el-input>
        <el-badge value="×">
          <el-upload
            el-upload
            action="#"
            class="avatar-uploader"
            :show-file-list="false"
          >
            <img v-if="item.img" :src="item.img" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon">上传图片</i>
          </el-upload>
        </el-badge>
      </div>
    </el-radio-group>
    <el-button type="danger" disabled style="display: block"
      >+添加选项</el-button
    >
  </el-form-item>
</template>

<script>
export default {
  name: 'radioVue',
  props: {
    value: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      radioItem: '',
      imageUrl: '',
      optionList: [
        {
          code: 'A',
          title: '',
          img: '',
          isRight: false
        },
        {
          code: 'B',
          title: '',
          img: '',
          isRight: false
        },
        {
          code: 'C',
          title: '',
          img: '',
          isRight: false
        },
        {
          code: 'D',
          title: '',
          img: '',
          isRight: false
        }
      ]
    }
  },
  watch: {
    value: function () {
      this.optionList = this.value
      this.optionList.forEach(item => {
        if (item.isRight === 1) {
          this.radioItem = item.code
        }
      })
    }
  },
  methods: {
    radioChange () {
      this.optionList.forEach((item) => {
        item.isRight = 0
        if (item.code === this.radioItem) {
          item.isRight = 1
        }
      })
      // this.value = this.optionList
      this.$emit('input', this.optionList)
    }
  }
}
</script>

<style lang='less' scoped>
.item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}
.inputbox {
  width: 300px;
  margin-right: 5px;
}
.avatar-uploader {
  width: 100px;
  height: 50px;
}
.el-upload--text {
  width: 100px;
  height: 50px;
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 14px;
  color: #8c939d;
  width: 100px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
/deep/ .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
